package com.example.maybeaccount.Mode;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.TimeZone;

/**
 * 服务，用于在后台判断是否需要提醒还款
 */
public class AlarmService extends Service {
    private AlarmManager am;
    private PendingIntent pi;
    private Long time;
    private String title;
    private String content;
    private SqliteDB mRemindSQL;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        getAlarmTime();
        return START_REDELIVER_INTENT;    } //这里为了提高优先级，选择START_REDELIVER_INTENT 没那么容易被内存清理时杀死
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    public void getAlarmTime() {
        /** database **/
        //获取当前时间
        TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
        Calendar calNow = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        calNow.set(Calendar.HOUR_OF_DAY, calNow.get(Calendar.HOUR_OF_DAY));
        //Calendar calNow = Calendar.getInstance();
        //calNow.setTimeInMillis(System.currentTimeMillis());
        //SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        //Toast.makeText(getApplicationContext(),"cal=" + df.format(calNow.getTime()),Toast.LENGTH_LONG).show();

        ArrayList<Item> itemList = SqliteDB.getInstance(this).getAllItem();
        ItemManager itemManager = new ItemManager();
        itemManager.setItem(itemList);
        Calendar payBackCalender = itemManager.getPayBackTime();
        if(payBackCalender.getTimeInMillis() != 0){
            if(payBackCalender.getTimeInMillis() < calNow.getTimeInMillis()
                    || (payBackCalender.getTimeInMillis()/1000/60/60/24 - calNow.getTimeInMillis()/1000/60/60/24) <= 5){
                title = "还款提醒";
                content = "该还款了，打工人 😐";
                Intent startNotification = new Intent(this, AlarmReceiver.class);   //启动的广播
                startNotification.putExtra("title", title);
                startNotification.putExtra("content", content);
                am = (AlarmManager) getSystemService(ALARM_SERVICE);   //系统闹钟的对象
                pi = PendingIntent.getBroadcast(this, 0, startNotification, PendingIntent.FLAG_UPDATE_CURRENT);     //设置事件
                //Long secondsNextEarlyMorning = getSecondsNextEarlyMorning(8);
                /** 还款间隔 */
                am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 30 * 60 * 1000, pi);    //提交事件，发送给 广播接收器

            }
        }else {
            //当无需还款时
            stopService(new Intent(this, AlarmService.class));
        }
    }

    //计算时间差，暂时无用
    public Long getSecondsNextEarlyMorning(int num) {
        Calendar cal = Calendar.getInstance();
        if (cal.get(Calendar.HOUR_OF_DAY) - num >= 0) {
            //如果当前时间大于等于8点 就计算第二天的8点的
            cal.add(Calendar.DAY_OF_YEAR, 1);
        } else {
            cal.add(Calendar.DAY_OF_YEAR, 0);
        }
        cal.set(Calendar.HOUR_OF_DAY, num);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        Long seconds = (cal.getTimeInMillis() - System.currentTimeMillis());

        return seconds.longValue();
    }

}